package com.library.bookmanagement.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.bookmanagement.entity.Admin;
import com.library.bookmanagement.entity.SuperAdmin;
import com.library.bookmanagement.service.SuperAdminService;
import com.library.bookmanagement.util.Constant;
import com.library.bookmanagement.util.PasswordUtil;
import com.library.bookmanagement.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.Result;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：ZXM
 * @date ：Created in 2020/5/9 9:10
 * @description：超级管理员控制层
 * 测试通过
 */
@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@ResponseBody
@RequestMapping("/superAdmin")
@Api(value = "超级管理Controller",tags = {"超级管理接口"})
public class SuperAdminController {
    @Autowired
    private SuperAdminService superAdminService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
    * @Description: 管理员用户添加（单独添加）
    * @Param: [name, password, email]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "管理员用户添加（单独添加）")
    @PostMapping("/adminAdd")
    public ResultUtil addAdmin(
            @ApiParam(value = "管理员名称",name = "name")
            @RequestParam(value = "name") String name,
            @ApiParam(value = "管理员密码",name = "password")
            @RequestParam(value = "password") String password,
            @ApiParam(value = "管理员邮箱",name = "email")
            @RequestParam(value = "email") String email
    ){
        String encode = bCryptPasswordEncoder.encode(password);
        Admin admin = new Admin();
        admin.setRole(2);
        admin.setName(name);
        admin.setPassword(encode);
        admin.setaEmail(email);
        admin.setImgUrl(Constant.EDIT_IMG_NAME);
        admin.setCreateTime(new Date(System.currentTimeMillis()));
        admin.setLastTime(admin.getCreateTime());
        return superAdminService.addAdmin(admin);
    }

    /**
    * @Description: 返回已选管理员信息
    * @Param: [adminId]
    * @return: com.library.bookmanagement.util.ResultUtil<com.library.bookmanagement.entity.Admin>
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @ApiOperation(value = "已选编辑管理员信息返回")
    @GetMapping("/adminReturn")
    public ResultUtil<Admin> queryAdminById(
            @ApiParam(value = "管理员ID",name = "adminId")
            @RequestParam(name = "adminId") Integer adminId
    ){
        return superAdminService.queryAdminById(adminId);
    }

    /**
    * @Description: 管理员基本信息修改保存
    * @Param: [admin]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @ApiOperation(value = "管理员基本信息修改保存")
    @PostMapping("/editAdminInfo")
    public ResultUtil editAdminInfo(
            @ApiParam(value = "管理员修改后对象",name = "admin")
            @RequestBody Admin admin
    ){
        return superAdminService.editAdminInfo(admin);
    }

    /**
    * @Description: 重置管理员密码
    * @Param: [adminId]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @ApiOperation("重置管理员密码")
    @PostMapping("/editPassword")
    public ResultUtil editPassword(
            @ApiParam(value = "管理员ID",name = "adminId")
            @RequestParam(name = "adminId") Integer adminId
    ){
        return superAdminService.editAdminPassword(adminId);
    }

    /**
    * @Description: 所用管理员查询结果返回
    * @Param: [pageNum]
    * @return: com.library.bookmanagement.util.ResultUtil<java.util.Map<java.lang.String,java.lang.Object>>
    * @Author: ZXM
    * @Date: 2020/6/10
    */
    @ApiOperation(value = "所用管理员查询结果返回")
    @GetMapping("/queryAll")
    public ResultUtil<Map<String,Object>> queryAll(
            @ApiParam(value = "第几页",name = "pageNum")
            @RequestParam(name = "pageNum") Integer pageNum
            ){
        return superAdminService.QueryAdminAll(pageNum, Constant.pageSize);
    }


    /**
    * @Description: 管理员删除(包括批量)
    * @Param: [ids]
    * @return: com.library.bookmanagement.util.ResultUtil
    * @Author: ZXM
    * @Date: 2020/6/19
    */
    @ApiOperation(value = "管理员删除(包括批量)")
    @GetMapping("/deleteAdminBy")
    public ResultUtil delete(
            @ApiParam(value = "删除ids",name = "ids")
            @RequestParam(value = "ids") List<Integer> ids
    ){
        return superAdminService.deleteById(ids);
    }
}
